<?php

namespace frontend\modules\touch\controllers;

use yii\web\Controller;
use common\components\Config;
use common\components\MobileDetect;
use backend\modules\home\models\Category;
use backend\modules\home\models\Article;
use backend\modules\home\models\Qikan;
use yii\data\Pagination;
use Yii;

/**
 * Default controller for the `touch` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public $enableCsrfValidation = false;
    public $layout = 'main';

    public function actionIndex()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/');
        }

        $catelist = Category::find()
        ->where(['cid' => 12])
        ->select('title,id')
        ->indexBy('id')
        ->column();


        return $this->render('index',['catelist' => $catelist]);

    }

    public function actionList($id){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/list?id='.$id);
        }

        $query = Article::find()->where([
            'cid' => $id,
            'type' => 1,
            'status' => 1,
        ]);

        $query->orderBy([
            'order' => SORT_DESC,
            'id' => SORT_DESC,
        ]);

        $total_count = $query->count();
        $pages = new Pagination(['totalCount' =>$total_count, 'pageSize' => '10']);
        $list = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();

        return $this->render('list',[
            'list' => $list,
            'pages' => $pages,
        ]);
    }

    public function actionView($id){
        $this->layout = false;
        $query = Article::find()->where([
            'id' => $id,
            'status' => 1,
        ])->one();

        if($query->cid == 28){
            $is_desc_display = true;
        }else{
            $is_desc_display = false;
        }

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/view?id='.$id);
        }
        return $this->render('view',[
            'model' => $query,
            'is_desc_display' => $is_desc_display,
        ]);
        
    }

    public function actionAbout()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/about');
        }

        $about = Article::find()->where(['id' => 1])->one();


        return $this->render('about',['about' => $about]);
    }

    public function actionContact()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/contact');
        }

        $model = Article::find()->where(['id' => 3])->one();
        return $this->render('contact',['model' => $model]);
    }

    public function actionNews()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/news');
        }

        $catelist = Category::find()
            ->where(['cid' => 12])
            ->select('title,id')
            ->indexBy('id')
            ->column();

        return $this->render('news',['catelist' => $catelist]);
    }

    public function actionService()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/service');
        }
        $model = Article::find()->where(['id' => 4])->one();
        return $this->render('service',['model' => $model]);
    }

    public function actionStep()
    {

        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/step');
        }
        $model = Article::find()->where(['id' => 5])->one();
        return $this->render('step', ['model' => $model]);
    }

    public function actionPrice(){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/price');
        }
        $model = Article::find()->where(['id' => 18])->one();
        return $this->render('price', ['model' => $model]);
    }

    public function actionResource(){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/resource');
        }
        $query = Qikan::find()
            ->where(['status' => 1])
            ->select('id,title,image_path,order')->orderBy([
                'order' => SORT_DESC,
                'created_at' => SORT_DESC,
            ]);
        $total_count = $query->count();
        $pages = new Pagination(['totalCount' =>$total_count, 'pageSize' => '10']);
        $list = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();

        return $this->render('resource',[
            'list' => $list,
            'pages' => $pages,
            'count' => $total_count,
        ]);
    }

    public function actionResourceView($id){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/home/default/resource-view?id='.$id);
        }
        $model = Qikan::findOne($id);

        return $this->render('resource-view',[
            'model' => $model,
        ]);
    }

    public function actionSitemap(){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect('/sitemap.html');
        }
        $query = Article::find()->where([
            'status' => 1,
        ]);

        $query->orderBy([
            'order' => SORT_DESC,
            'id' => SORT_DESC,
        ]);
        $article = $query->asArray()->all();
        $qikan = Qikan::find()
            ->where(['status' => 1])
            ->select('id,title,image_path,order')->orderBy([
                'order' => SORT_DESC,
                'created_at' => SORT_DESC,
            ])
            ->asArray()
            ->all();

        return $this->render('sitemap', [
            'article' => $article,
            'qikan' => $qikan
        ]);
    }


}
